﻿Public Class FrmModifMdpAdmin

    Dim macnx As New PPE3.ClassMySqlConnect("853339_depanneur", "071064")
    Dim d As Integer = 0
    Dim modif As Boolean

    Public Sub chargement()
        macnx.Se_Connecter_MySqlServer()
        macnx.viderVue("Compte")
        DataGridView1.DataSource = Nothing
        DataGridView1.DataSource = macnx.Executer_Requete_Select("SELECT idCompte , NomCompte , MDPCompte FROM compte WHERE isAdmin = True", "Compte")
        macnx.Se_Deconnecter_MySqlServer()
        If DataGridView1.RowCount - 1 <> 0 Then

            tbNom.Text = DataGridView1.Rows(0).Cells(1).Value.ToString()
            tbMDP.Text = DataGridView1.Rows(0).Cells(2).Value.ToString()

        End If
    End Sub

    Private Sub FrmModifMdpAdmin_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = FrmStart
        Me.CenterToScreen()

        chargement()
    End Sub

    Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If DataGridView1.RowCount - 1 <> 0 Then

            tbNom.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
            tbMDP.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()

        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnAjouter.Click
        If btnAjouter.Text = "Ajouter" Then

            tbNom.Text = ""
            tbMDP.Text = ""

            DataGridView1.Enabled = False

            tbMDP.Enabled = True
            tbNom.Enabled = True

            btnModifier.Visible = False

            btnAjouter.Text = "Valider"
            btnSupprimer.Text = "Annuler"

            btnAjouter.BackColor = Color.Green
            btnSupprimer.BackColor = Color.Red

        Else

            If (tbMDP.Text <> "") And (tbNom.Text <> "") Then

                DataGridView1.Enabled = True

                tbMDP.Enabled = False
                tbNom.Enabled = False

                btnModifier.Visible = True

                btnAjouter.Text = "Ajouter"
                btnSupprimer.Text = "Supprimer"

                btnAjouter.BackColor = DefaultBackColor
                btnSupprimer.BackColor = DefaultBackColor

                macnx.Se_Connecter_MySqlServer()

                If modif = True Then

                    Dim id As String = DataGridView1.CurrentRow.Cells(0).Value.ToString

                    macnx.Executer_Requete_Action("UPDATE compte SET NomCompte = '" & tbNom.Text & "' , MDPCompte = '" & tbMDP.Text & "' WHERE idCompte = '" & id & "' ")

                    modif = False

                Else

                    Dim id As String = macnx.Generer_Cle_Primaire("idCompte", "compte")

                    macnx.Executer_Requete_Action("INSERT INTO compte(idCompte, NomCompte, MDPCompte, isAdmin) VALUES ('" & id & "' , '" & tbNom.Text & "' , '" & tbMDP.Text & "' , True )")

                End If
                macnx.Se_Deconnecter_MySqlServer()
                chargement()
            Else
                MessageBox.Show("Veuiller rentrer un Nom de compte et un Mot de Passe !")
            End If
        End If
    End Sub

    Private Sub btnSupprimer_Click(sender As System.Object, e As System.EventArgs) Handles btnSupprimer.Click
        If btnSupprimer.Text = "Supprimer" Then
            Dim nom As String = tbNom.Text
            Dim mdp As String = tbMDP.Text
            If (MessageBox.Show("Etes vous sur de vouloir supprimer l'utilisateur " & nom & " ???", "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then

                macnx.Se_Connecter_MySqlServer()

                macnx.Executer_Requete_Action("DELETE FROM compte WHERE NomCompte = '" & nom & "' ")

                macnx.Se_Deconnecter_MySqlServer()

                chargement()

            End If

        Else
            DataGridView1.Enabled = True

            tbMDP.Enabled = False
            tbNom.Enabled = False

            btnModifier.Visible = True

            btnAjouter.Text = "Ajouter"
            btnSupprimer.Text = "Supprimer"

            btnAjouter.BackColor = DefaultBackColor
            btnSupprimer.BackColor = DefaultBackColor

            chargement()
        End If
    End Sub

    Private Sub btnModifier_Click(sender As System.Object, e As System.EventArgs) Handles btnModifier.Click
        modif = True

        DataGridView1.Enabled = False

        tbMDP.Enabled = True
        tbNom.Enabled = True

        btnModifier.Visible = False

        btnAjouter.Text = "Valider"
        btnSupprimer.Text = "Annuler"

        btnAjouter.BackColor = Color.Green
        btnSupprimer.BackColor = Color.Red


    End Sub
End Class